import ctypes
import datetime
import random
import time
import pyautogui
import schedule
import win32con
import win32gui
import win32api
from schedule import every, repeat, run_pending
from check_pic import check_pic
from check_pic import check_share

hwnd = win32gui.FindWindow(None, "动物餐厅")  # 父句柄
hwndChildList = []
win32gui.EnumChildWindows(hwnd, lambda hwnd, param: param.append(hwnd), hwndChildList)
hwnd1 = win32gui.FindWindowEx(hwndChildList[0], 0, None, None)
win32gui.SetForegroundWindow(hwnd)  # 指定句柄设置为前台，也就是激活
win32gui.ShowWindow(hwnd, win32con.SW_NORMAL)  # 显示最前 从最小化模式显示为正常
windowRec = win32gui.GetWindowRect(hwnd1)  # 目标子句柄窗口的坐标 左上右下边到屏幕边的距离
left, top = windowRec[0], windowRec[1]
width, height = windowRec[2] - windowRec[0], windowRec[3] - windowRec[1]
# print(windowRec)
t = random.randint(40, 100) / 100

[watch_ad_x, watch_ad_y] = [293, 399]  # 卡广告获得按钮位置
[close_x, close_y] = [415, 39]  # 广告关闭按钮位置
[cg_place_x, cg_place_y] = [39, 159]  # 切换场景窗口位置
[exit_cg_x, exit_cg_y] = [45, 399]  # 退出场景窗口
[yield_place_x, yield_place_y] = [204, 292]  # 前院小窗口
[cg_left_x, cg_left_y] = [23, 394]  # 前院向左移
[cg_right_x, cg_right_y] = [423, 396]  # 前院向右移
[actor1_x, actor1_y] = [193, 332]  # 嘉宾1位置
[actor2_x, actor2_y] = [345, 325]  # 嘉宾2位置
[actor3_x, actor3_y] = [117, 380]  # 嘉宾3位置
[actor4_x, actor4_y] = [273, 401]  # 嘉宾4位置
[go_outside_x, go_outside_y] = [292, 629]
[ensure_outside_x, ensure_outside_y] = [293, 420]
[get_card_x, get_card_y] = [164, 626]  # 获得卡片入口
[invite_enter_x, invite_enter_y] = [138, 137]  # 邀请卡片 8
[summon_enter_x, summon_enter_y] = [315, 134]  # 召唤卡 4
[exit_card_x, exit_card_y] = [224, 705]  # 退出卡片窗口
[roof_place_x, roof_place_y] = [313, 305]  # 天台小窗口
[sideboard_x, sideboard_y] = [134, 226]  # 餐边柜
[bottle_enter_x, bottle_enter_y] = [231, 98]  # 获得瓶子入口
[fishermen_place_x, fishermen_place_y] = [105, 371]  # 切换钓鱼小窗口
[pond_x, pond_y] = [233, 397]  # 池塘位置
[bait_add_x, bait_add_y] = [223, 97]  # 池塘添加鱼饵按钮位置
[close_pond_x, close_pond_y] = [40, 754]  # 关闭池塘按钮位置
[garden_place_x, garden_place_y] = [138, 453]  # 切换到花园
[hall_place_x, hall_place_y] = [224, 380]  # 切换到大厅
[wish_x, wish_y] = [227, 573]  # 许愿池位置
[agree_x, agree_y] = [170, 505]  # 许愿池我愿意按钮位置
[leave_animal_x, leave_animal_y] = [517, 361]  # 离开游戏总窗口
[share_one_x, share_one_y] = [282, 484]  # 分享键
[fifteenth_broadcast_x, fifteenth_broadcast_y] = [318, 730]  # 十五次宣传
[daisy_x, daisy_y] = [136, 361]  # 小雏菊
[sunflower_x, sunflower_y] = [310, 362]  # 向日葵
[rose_x, rose_y] = [129, 476]  # 玫瑰
[violet_x, violet_y] = [302, 472]  # 紫罗兰
[sow_flower_x, sow_flower_y] = [223, 497]
[money_of_postbox_x, money_of_postbox_y] = [48, 223]
[money_of_grill_x, money_of_grill_y] = [294, 179]
[broadcast_x, broadcast_y] = [400, 730]  # 手机宣传按钮在屏幕中的位置
[magnifier_x, magnifier_y] = [130, 740]
[TV_x, TV_y] = [60, 260]
[TV_slide_x, TV_slide_y] = [220, 615]
[another_visitor_x, another_visitor_y] = [47, 656]
[use_magnifier_x, use_magnifier_y] = [150, 510]
[go_home_x, go_home_y] = [50, 732]


class Function:
    @staticmethod
    def Doclick1(pos, m=0):
        global t
        t = random.randint(40, 100) / 100
        # tmp = [left + pos[0], top + pos[1]]
        # win32api.SetCursorPos(tmp)
        # ctypes.windll.user32.SetCursorPos(tmp[0], tmp[1])
        # ctypes.windll.user32.mouse_event(2, 0, 0, 0, 0)
        # ctypes.windll.user32.mouse_event(4, 0, 0, 0, 0)
        tmp = [int(pos[0]), int(pos[1])]
        tmp = win32api.MAKELONG(tmp[0], tmp[1])
        win32gui.SendMessage(hwnd1, win32con.WM_ACTIVATE, win32con.WA_CLICKACTIVE, 0)  # 激活窗口
        win32gui.SendMessage(hwnd1, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, tmp)  # 鼠标左键按下
        # time.sleep(0.01)
        win32gui.SendMessage(hwnd1, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, tmp)  # 鼠标左键松开
        t = m if m else t
        time.sleep(t)

    def check_adv_method(self):  # 检测获得奖励是通过看广告还是分享
        if check_pic("share_picture1", 0.9):
            self.Doclick1([share_one_x, share_one_y])
            check_share("close_share")
        else:
            # 等待广告时间30s
            time.sleep(16)
            self.check_adv()

    def wait_advert(self, count):  # 广告奖励物获得 诱饵 卡片
        i = 0
        while i < count:
            print('获得奖励第%d次' % (i + 1))
            print('time:' + time.asctime(time.localtime(time.time())))
            # 暂停
            time.sleep(random.randint(1, 2))
            # 点击看广告获取
            self.Doclick1([watch_ad_x, watch_ad_y])
            time.sleep(0.01)
            self.Doclick1([watch_ad_x, watch_ad_y])
            self.check_adv_method()
            i = i + 1
            if i == count:
                break
            # 等待刷新及休息时间
            time.sleep(random.randint(5, 10))

    def check_adv(self):  # 广告检测
        wait_time = 16
        while True:
            if check_pic("finish", 0.9):  # 检测广告是否结束
                # 点击关闭
                self.Doclick1([close_x, close_y])
                break
            elif wait_time <= 31:
                wait_time += 13
                time.sleep(13)
                if check_pic("finish", 0.8):
                    # 点击关闭
                    self.Doclick1([close_x, close_y])
                    break
                else:
                    wait_time += 1.25
                    time.sleep(1.25)
            else:  # 如果openCV太菜了检测不出来就自己关闭
                self.Doclick1([close_x, close_y])
                break

    def refresh(self):
        self.Doclick1([cg_place_x, cg_place_y])  # 切换场景窗口位置
        self.Doclick1([hall_place_x, hall_place_y])  # 切换到大厅
        time.sleep(1)
        self.Doclick1([exit_cg_x, exit_cg_y])  # 退出场景
        # win32api.SetCursorPos([leave_animal_x, leave_animal_y])  # 离开游戏总窗口

    def repeat_check(self, real_img):
        while True:
            if check_pic(str(real_img), 0.9):
                self.wait_advert(1)
            else:
                break

    def go_homepage(self):  # 通过中国结判断是不是主页
        if check_pic("chinese_knot" or "lucky_dog", 0.9):
            # win32api.SetCursorPos([leave_animal_x, leave_animal_y])  # 离开游戏总窗口
            pass
        else:
            self.refresh()


class Yield(Function):  # 庭院工作
    def enter_card_gain(self):
        self.Doclick1([cg_place_x, cg_place_y])  # 切换场景窗口位置
        self.Doclick1([yield_place_x, yield_place_y])  # 前院小窗口
        time.sleep(3)  # 转场缓冲
        self.Doclick1([cg_left_x, cg_left_y])  # 前院向左移
        self.Doclick1([actor1_x, actor1_y])  # 嘉宾位置

    def get_invite_card(self):
        self.Doclick1([get_card_x, get_card_y])  # 进入获得邀请卡界面
        time.sleep(0.5)
        self.repeat_check("real_invite_card")
        self.Doclick1([summon_enter_x, summon_enter_y])  # 退出获得邀请卡界面

    def get_summon_card(self):
        self.Doclick1([summon_enter_x, summon_enter_y])  # 进入召唤卡界面
        self.Doclick1([get_card_x, get_card_y])  # 进入获得召唤卡界面
        time.sleep(0.5)
        self.repeat_check("real_summon_card")
        self.Doclick1([exit_card_x, exit_card_y])

    def exit_card_gain(self):
        self.Doclick1([exit_card_x, exit_card_y])

    def total_card(self):
        pyautogui.FAILSAFE = True
        # pyautogui.alert('开始运行拿卡程序')  # 废话
        print('开始运行拿卡程序')
        self.enter_card_gain()
        print('程序正在运行...')
        self.get_invite_card()  # 获得邀请卡
        self.get_summon_card()  # 获得召唤卡
        self.exit_card_gain()
        self.refresh()


class Bottle(Function):
    def enter_bottle_gain(self):
        self.Doclick1([cg_place_x, cg_place_y])  # 切换场景窗口位置
        self.Doclick1([roof_place_x, roof_place_y])  # 天台小窗口
        time.sleep(3)  # 转场缓冲
        self.Doclick1([exit_cg_x, exit_cg_y])
        self.Doclick1([bottle_enter_x, bottle_enter_y])
        time.sleep(0.5)  # 瓶子缓冲

    def exit_bottle_gain(self):
        self.Doclick1([cg_place_x + 10, cg_place_y + 10])  # 切换场景窗口位置

    def total_bottle(self):
        pyautogui.FAILSAFE = True
        # pyautogui.alert('开始运行拿瓶子程序')  # 废话
        print("开始运行拿瓶子程序")
        self.enter_bottle_gain()
        print('程序正在运行...')
        self.repeat_check("real_bottle")
        self.exit_bottle_gain()
        self.refresh()


class Fish(Function):
    def enter_bait_gain(self):
        self.Doclick1([cg_place_x, cg_place_y])  # 切换场景窗口位置
        self.Doclick1([fishermen_place_x, fishermen_place_y])  # 切换钓鱼小窗口
        self.Doclick1([exit_cg_x, exit_cg_y])
        self.Doclick1([pond_x, pond_y])
        self.Doclick1([bait_add_x, bait_add_y])
        time.sleep(0.5)

    def exit_bait_gain(self):
        self.Doclick1([close_pond_x, close_pond_y])
        time.sleep(0.01)
        self.Doclick1([close_pond_x, close_pond_y])

    def total_bait(self):
        pyautogui.FAILSAFE = True
        # pyautogui.alert('开始运行钓饵程序')  # 废话
        print("开始运行钓饵程序")
        self.enter_bait_gain()  # 钓饵 上限 21
        print('程序正在运行...')
        self.repeat_check("real_bait")
        self.exit_bait_gain()
        self.refresh()


class Spring(Function):
    def enter_garden(self):
        self.Doclick1([cg_place_x, cg_place_y])  # 切换场景窗口位置
        self.Doclick1([garden_place_x, garden_place_y])  # 切换到花园
        self.Doclick1([exit_cg_x, exit_cg_y])

    def wish_adv(self):
        # 点击我愿意
        self.Doclick1([agree_x, agree_y])
        self.check_adv_method()

    def dream_realize(self, count):
        i = 0
        while i < count:
            # 点击许愿池
            self.Doclick1([wish_x, wish_y])
            print('运行第%d次' % (i + 1))
            print('time:' + time.asctime(time.localtime(time.time())))
            if check_pic("wish_out", 0.9):
                break
            elif check_pic("share_picture", 0.8):
                self.Doclick1([agree_x, agree_y])
                time.sleep(1)
                check_share("close_share")
            else:
                self.wish_adv()
            i = i + 1
            if i == count:
                break
            # 等待许愿及休息时间
            time.sleep(random.randint(30, 32))

    def exit_garden(self):
        self.Doclick1([cg_right_x, cg_right_y])
        self.Doclick1([cg_right_x, cg_right_y])
        # win32api.SetCursorPos([left + leave_animal_x, top + leave_animal_y])

    def total_wish(self):
        self.enter_garden()
        self.dream_realize(35)
        self.exit_garden()
        # self.refresh()


class Broadcast_Fifteenth(Function):
    def enter_broadcast(self):
        if check_pic("broadcast_av", 0.9):
            self.Doclick1([fifteenth_broadcast_x, fifteenth_broadcast_y])  # 点击看广告的宣传键
            self.Doclick1([agree_x, agree_y])  # 点击同意
            # 等待广告时间35s
            time.sleep(16)
            self.check_adv()
        elif check_pic("broadcast_share", 0.9):
            self.Doclick1([fifteenth_broadcast_x, fifteenth_broadcast_y])
            if check_pic("finish_broadcast", 0.9):
                self.Doclick1([fifteenth_broadcast_x, fifteenth_broadcast_y])
            else:
                self.Doclick1([agree_x, agree_y])
        else:
            self.Doclick1([cg_place_x, cg_place_y])  # 切换场景窗口位置
            self.Doclick1([hall_place_x, hall_place_y])  # 切换到大厅
            time.sleep(3)
            self.enter_broadcast()

    def check_broadcast(self):
        if check_pic("share_picture1", 0.9):  # 点观看后可能有第二个分享图片
            self.Doclick1([share_one_x, share_one_y])
            check_share("close_share")
        else:
            check_share("close_share")

    def total_leaflet(self):
        self.enter_broadcast()
        self.check_broadcast()
        self.go_homepage()


class Flower(Function):
    def enter_garden(self):
        self.Doclick1([cg_place_x, cg_place_y])  # 切换场景窗口位置
        self.Doclick1([garden_place_x, garden_place_y])  # 切换到花园
        self.Doclick1([exit_cg_x, exit_cg_y])

    def sow(self, seed):
        if seed == "daisy":
            self.Doclick1([daisy_x, daisy_y])
            self.Doclick1([sow_flower_x, sow_flower_y])
        elif seed == "sunflower":
            self.Doclick1([sunflower_x, sunflower_y])
            self.Doclick1([sow_flower_x, sow_flower_y])
        elif seed == "rose":
            self.Doclick1([rose_x, rose_y])
            self.Doclick1([sow_flower_x, sow_flower_y])
        elif seed == "violet":
            self.Doclick1([violet_x, violet_y])
            self.Doclick1([sow_flower_x, sow_flower_y])

    def check_ripe(self):
        for i in range(4):
            if check_pic("daisy", 0.9):
                self.Doclick1([daisy_x, daisy_y])
                time.sleep(2)
                self.sow("daisy")
            elif check_pic("sunflower", 0.9):
                self.Doclick1([sunflower_x, sunflower_y])
                time.sleep(2)
                self.sow("sunflower")
            elif check_pic("rose", 0.9):
                self.Doclick1([rose_x, rose_y])
                time.sleep(2)
                self.sow("rose")
            elif check_pic("violet", 0.9):
                self.Doclick1([violet_x, violet_y])
                time.sleep(2)
                self.sow("violet")
            else:
                break

    def check_what_sow(self):
        if check_pic("daisy_wait_sow", 0.9):
            self.sow("daisy")
        elif check_pic("sunflower_wait_sow", 0.9):
            self.sow("sunflower")
        elif check_pic("rose_wait_sow", 0.99):
            self.sow("rose")
        elif check_pic("violet_wait_sow", 0.99):
            self.sow("violet")
        else:
            pass

    def check_sow(self):
        time.sleep(5)
        for i in range(4):
            if check_pic("wait_sow", 0.9):
                self.check_what_sow()
            else:
                break

    def exit_garden(self):
        self.Doclick1([cg_right_x, cg_right_y])
        # win32api.SetCursorPos([left + leave_animal_x, top + leave_animal_y])

    def gain_garden_interest(self):
        self.Doclick1([money_of_grill_x, money_of_grill_y])
        time.sleep(0.1)
        self.Doclick1([money_of_postbox_x, money_of_postbox_y])

    def total_harvest(self):
        self.enter_garden()
        self.gain_garden_interest()
        self.check_ripe()
        self.check_sow()
        self.exit_garden()


class Star(Yield):

    def enter_star_stage(self):
        self.Doclick1([cg_place_x, cg_place_y])  # 切换场景窗口位置
        self.Doclick1([yield_place_x, yield_place_y])  # 前院小窗口
        time.sleep(3)  # 转场缓冲
        self.Doclick1([cg_left_x, cg_left_y])  # 前院向左移

    def permission_outside(self):
        self.Doclick1([ensure_outside_x, ensure_outside_y])
        self.Doclick1([exit_card_x, exit_card_y])

    def check_star_exist(self):
        actor_total = [[actor1_x, actor1_y], [actor2_x, actor2_y],
                       [actor3_x, actor3_y], [actor4_x, actor4_y]]
        for actor in actor_total:
            self.Doclick1(actor)  # 嘉宾位置
            if check_pic("show_over", 0.99):
                self.Doclick1([go_outside_x, go_outside_y])
                if check_pic("invite_card_out", 0.99):
                    self.Doclick1([exit_card_x, exit_card_y])
                    self.Doclick1([exit_card_x, exit_card_y])
                    break
                elif check_pic("need_invite_card", 0.9):
                    self.Doclick1([exit_card_x, exit_card_y])
                    self.get_invite_card()
                    self.Doclick1([go_outside_x, go_outside_y])
                    self.permission_outside()
                else:
                    self.permission_outside()
            else:
                self.Doclick1([exit_card_x, exit_card_y])
                pass

    def total_show(self):
        self.enter_star_stage()
        self.check_star_exist()
        self.refresh()

class Work:
    @staticmethod
    def daily_work():
        yield1 = Yield()
        yield1.total_card()
        star1 = Star()
        star1.total_show()
        visitor = Visit()
        visitor.visit_total()
        bottle1 = Bottle()
        bottle1.total_bottle()
        fish1 = Fish()
        fish1.total_bait()
        spring1 = Spring()
        spring1.total_wish()

    @staticmethod
    def flower_work():
        flower1 = Flower()
        flower1.total_harvest()

    @staticmethod
    def broadcast_work():
        broadcast1 = Broadcast_Fifteenth()
        broadcast1.total_leaflet()

    @staticmethod
    def locate_relativity():  # 鼠标定位函数
        while 1:
            tempt = win32api.GetCursorPos()  # 记录鼠标所处位置的坐标
            print(tempt)
            x = tempt[0] - windowRec[0]  # 计算相对x坐标
            y = tempt[1] - windowRec[1]  # 计算相对y坐标
            print(str(x) + ",", y)
            time.sleep(0.5)  # 每0.5s输出一次


if __name__ == '__main__':
    start = time.perf_counter()
     Work.daily_work()
    # Work.flower_work()
    # Work.broadcast_work()
    # Work.locate_relativity()  # 你们自己该定位时候需要
    end = time.perf_counter()
    print(end - start)




